#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
mutex              mtx;
bool               flag = false;
condition_variable cv;

void PrintId(int id) {
    unique_lock<mutex> lock(mtx);
    while (!flag) {
        cv.wait_for(lock, std::chrono::seconds(3));
        break;
    }
    cout << id << endl;
}

int main() {
    thread th[10];
    for (int i = 0; i < 10; ++i) {
        th[i] = thread(PrintId, i);
    }

    this_thread::sleep_for(chrono::seconds(5));
    flag = true;
    //    cv.notify_all();
    for (auto& i : th)
        i.join();
    return 0;
}
